import os
import datetime
from time import sleep
from selenium.webdriver.common.by import By
from common.readaccountinfo import ReadConfig
from common.readsql import read_sql
from page.mainpage import MainPage
from common.readpath import ReadPath


class ClassManagement(MainPage):

    def goto_classmanagemant(self):
        """
        进入侧导航栏排序第一的县级的学校管理
        打开第一所学校管理菜单
        进入班级管理
        :return:
        """
        self.click("css", '#app > div > section > div > div:nth-child(2) > section > ul > li:nth-child(1)')
        self.click("css",
                   '#app > div > section > div > div:nth-child(2) > section > ul > li.secondstage.el-submenu.is-active.is-opened > ul > li:nth-child(1)')
        self.click("xp", "//*[text()='班级管理']")
        sleep(1)

    def locate_addclass_button(self):
        """
        定位新增班级按钮
        :return:
        """
        self.locate("css", "div.options > div.actionsgroup > button:nth-child(3)")

    def add_class(self, gradename, classname, teacher):
        """
        点击新增班级按钮
        :param gradename: 年级名
        :param classname: 班级名
        :param teacher: 班主任
        :return:
        """
        self.click("css", "div.options > div.actionsgroup > button:nth-child(3)")
        self.send_keys("css",
                       "div.main-content > section > section > section > section > div:nth-child(1) > div > input",
                       gradename)
        self.send_keys("css",
                       "div.main-content > section > section > section > section > div:nth-child(2) > div > input",
                       classname)
        if teacher == "":
            self.click_submit()
        else:
            self.click("css",
                       "div.main-content > section > section > section > section > div:nth-child(3) > div > div > input")  # 打开教师选择下拉框
            self.send_keys("css",
                           "div.main-content > section > section > section > section > div:nth-child(3) > div > div > input",
                           teacher)
            self.click("xp", f"//*[text()='{teacher}']")
            self.click_submit()

    def edit_class(self, gradename, classname, teacher):
        """
        编辑班级列表第一个班级
        :param gradename: 年级名
        :param classname: 班级名
        :param teacher: 班主任
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_6.is-center > div > button:nth-child(1)")
        self.send_keys("css",
                       "div.main-content > section > section > section > section > div:nth-child(1) > div > input",
                       gradename)
        self.send_keys("css",
                       "div.main-content > section > section > section > section > div:nth-child(2) > div > input",
                       classname)
        if teacher == "":
            self.click_submit()
        else:
            self.click("css",
                       "div.main-content > section > section > section > section > div:nth-child(3) > div > div > input")  # 打开教师选择下拉框
            self.send_keys("css",
                           "div.main-content > section > section > section > section > div:nth-child(3) > div > div > input",
                           teacher)
            self.click("xp", f"//*[text()='{teacher}']")
            self.click_submit()

    def delete_num1_class(self):
        """
        删除班级列表第一个班级
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(1) > td.el-table_1_column_6.is-center > div > button:nth-child(2)")

    def get_num1_gradename(self):
        """
        :return:获取班级列表第一位班级的年级名
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_2.is-center")

    def get_num1_classname(self):
        """
        获取班级列表第一位班级的班级名
        :return:
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_3.is-center")

    def get_num1_headmaster(self):
        """
        获取班级列表第一位班级的班主任
        :return:
        """
        return self.get_text("css",
                             "div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr:nth-child(1) > td.el-table_1_column_4.is-center")

    def goto_classschedule(self):
        """
        进入班级课表设置
        :return:
        """
        self.click("css",
                   "div.el-table__fixed-body-wrapper > table > tbody > tr.el-table__row.el-table__row--striped > td.el-table_1_column_6.is-center > div > button:nth-child(4)")

    def import_classschedule(self, file):
        """
        点击上传课表
        上传文件
        关闭文件上传后的提示框
        点击提交
        :param file:需要上传的文件名
        :return:
        """
        self.click("css", "#pane-0 > div.classOptions > div > div.actionsgroup > button:nth-child(2)")
        file_path = os.path.join(ReadPath().get_datafile_path(), file)
        # 上传文件(此处上传文件,不能用封装的.send_keys,因为里面包含其他的方法)
        self.driver.find_element(By.NAME, "file").send_keys(file_path)
        sleep(1)
        self.click_importpopup_submit()

    def import_temporaryschedule(self, file):
        """
        点击上传临时课表
        上传文件
        关闭文件上传后的提示框
        选择临时课表执行日期
        点击提交
        :param file:需要上传的文件名
        :return:
        """
        self.click("css", "div.classOptions > div > div.actionsgroup > button:nth-child(3)")
        file_path = os.path.join(ReadPath().get_datafile_path(), file)
        # 上传文件(此处上传文件,不能用封装的.send_keys,因为里面包含其他的方法)
        self.driver.find_element(By.NAME, "file").send_keys(file_path)
        TomorrowTime = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        self.send_keys("css",
                       "div:nth-child(5) > div > div.el-dialog__body > div > div.schedulebtn.schedulebtn1 > div > input",
                       TomorrowTime)
        self.click_importpopup_submit()

    def goto_classinfo(self):
        """
        进入班级信息设置
        :return:
        """
        self.click("css",
                   "#app > div > section > div > div:nth-child(2) > div > div.main-content > section > section > section > div.el-table.default-table.el-table--fit.el-table--striped.el-table--enable-row-transition.default-table2 > div.el-table__fixed-right > div.el-table__fixed-body-wrapper > table > tbody > tr.el-table__row.el-table__row--striped > td.el-table_1_column_6.is-center > div > button:nth-child(3)")

    def set_classmotto(self, classmotto):
        """
        输入班级口号
        点击确认
        :param classmotto: 班级口号
        :return:
        """
        self.send_keys("css", "#pane-0 > div:nth-child(2) > div.classOptions-div > div:nth-child(2) > input",
                       classmotto)
        self.click("css", "#pane-0 > div:nth-child(2) > div.classOptions-div > button:nth-child(3)")

    def set_sendword(self, sendword):
        """
        输入班主任寄语
        点击确认
        :param sendword:班主任寄语
        :return:
        """
        self.send_keys("css", "#pane-0 > div:nth-child(2) > div.classOptions-div > div:nth-child(5) > input", sendword)
        self.click("css", "#pane-0 > div:nth-child(2) > div.classOptions-div > button:nth-child(6)")

    def set_dutystudent(self):
        """
        选择日期(测试执行的明天)
        点击更改
        打开选择学生下拉框
        选择学生
        点击保存
        :return:
        """
        TomorrowTime = (datetime.datetime.now() + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
        self.send_keys("css",
                       "#pane-0 > div:nth-child(3) > div.classOptions-div > div > div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr > td.el-table_2_column_7.is-center > div > div > input",
                       TomorrowTime)
        self.click("css",
                   "#pane-0 > div:nth-child(3) > div.classOptions-div > div > div.el-table__fixed-right > div.el-table__fixed-body-wrapper > table > tbody > tr > td.el-table_2_column_9.is-center > div > button")
        self.click("css",
                   "#pane-0 > div:nth-child(3) > div.classOptions-div > div > div.el-table__body-wrapper.is-scrolling-none > table > tbody > tr > td.el-table_2_column_8.is-center > div > div > div.el-input.el-input--suffix > span > span > i")
        for i in range(1, 4):
            self.driver.execute_script(
                f'document.querySelector("body > div.el-select-dropdown.el-popper.is-multiple > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li:nth-child({i})").click()')
        self.click("css",
                   "#pane-0 > div:nth-child(3) > div.classOptions-div > div > div.el-table__fixed-right > div.el-table__fixed-body-wrapper > table > tbody > tr > td.el-table_2_column_9.is-center > div > button:nth-child(1)")

    def import_dutystudent(self, file):
        """
        点击导入（值日生）
        上传文件
        关闭文件上传后的提示框
        点击提交
        :param file:需要上传的文件名
        :return:
        """
        self.click("css", "#pane-0 > div:nth-child(3) > div.classOptions-title > div > button:nth-child(1)")
        file_path = os.path.join(ReadPath().get_datafile_path(), file)
        # 上传文件(此处上传文件,不能用封装的.send_keys,因为里面包含其他的方法)
        self.driver.find_element(By.NAME, "file").send_keys(file_path)
        sleep(1)
        self.close_popup()
        self.click_importpopup_submit()
        sleep(1)

    def auto_distribution_dutystudent(self):
        """
        点击智能分配
        :param startdate: 开始日期
        :param enddate: 结束日期
        :param num: 值日生数
        点击确认分配
        :return:
        """
        self.click("css", "#pane-0 > div:nth-child(3) > div.classOptions-title > div > button:nth-child(2)")
        startTime = (datetime.datetime.now() + datetime.timedelta(days=5)).strftime('%Y-%m-%d')
        self.send_keys("css",
                       "#pane-0 > div:nth-child(6) > div > div.el-dialog__body > div > div:nth-child(1) > div > input:nth-child(2)",
                       startTime)
        endtime = (datetime.datetime.now() + datetime.timedelta(days=7)).strftime('%Y-%m-%d')
        self.send_keys("css",
                       "#pane-0 > div:nth-child(6) > div > div.el-dialog__body > div > div:nth-child(1) > div > input:nth-child(4)",
                       endtime)
        self.click("css", "#pane-0 > div:nth-child(6) > div > div.el-dialog__header > span")
        self.send_keys("css",
                       "#pane-0 > div:nth-child(6) > div > div.el-dialog__body > div > div:nth-child(2) > div > input",
                       3)
        self.click("css",
                   "#pane-0 > div:nth-child(6) > div > div.el-dialog__body > div > div.edit-group > button.el-button.submitbtn.el-button--default")

    def set_classstar(self):
        """
        设置三位班级之星
        点击编辑
        打开下拉框
        选择学生
        输入班级之星描述
        点击确认
        :return:
        """
        for i in range(1, 4):
            self.click(f"css",
                       f"#pane-0 > div:nth-child(4) > div.classOptions-div > div > div.el-table__fixed-right > div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child({i}) > td.el-table_3_column_13.is-center > div > button")
            self.click(f"css",
                       "#pane-0 > div:nth-child(7) > div > div.el-dialog__body > div > div.ds-div > div > div.el-input.el-input--suffix > span > span > i")
            self.driver.execute_script(
                f'document.querySelector("body > div.el-select-dropdown.el-popper > div.el-scrollbar > div.el-select-dropdown__wrap.el-scrollbar__wrap > ul > li:nth-child({i})").click()')
            self.send_keys("cn", "w-e-text", f"我是年级第{i}")
            sleep(0.5)
            self.driver.execute_script(
                'document.querySelector("#pane-0 > div:nth-child(7) > div > div.el-dialog__body > div > div.edit-group > button.el-button.submitbtn.el-button--default").click()')

        # self.click("css","#pane-0 > div:nth-child(4) > div.classOptions-div > div > div.el-table__fixed-right > div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(2) > td.el-table_3_column_13.is-center > div > button")
        # self.click("css","#pane-0 > div:nth-child(4) > div.classOptions-div > div > div.el-table__fixed-right > div.el-table__fixed-body-wrapper > table > tbody > tr:nth-child(3) > td.el-table_3_column_13.is-center > div > button")

    def delete_dutystudent(self):
        """
        删除数据库值日生数据
        :return:
        """
        read_sql('delete from student_duty;')

    def backtrack(self):
        """
        返回班级管理页面
        :return:
        """
        self.click("css", "#pane-0 > div.classTitle > span:nth-child(3)")


class New_MyData(Base):
    def new_my_data(self, filetype, marktype):
        from test_alphamind.po.page.my_data import My_Data
        # 定位图片文件类型
        self.find(By.XPATH, f"//*[text()='{filetype}']").click()
        sleep(1)
        # 定位标注类型
        self.find(By.XPATH, f"//*[text()='{marktype}']").click()
        # 输入名称
        self.find(By.ID, 'name').send_keys(22)
        # 输入描述
        self.find(By.ID, 'descStr').send_keys('描述22')
        # 点击确定
        self.find(By.CSS_SELECTOR, '.ant-modal-content>div:nth-child(4)>button:nth-child(2)').click()
        return My_Data()


if __name__ == '__main__':
    # 打开谷歌浏览器,输入测试地址
    login = ClassManagement(ReadConfig().get_http('browsertype'), ReadConfig().get_http('url'))
    # 输入登录账号,密码
    login.login(ReadConfig().get_account('username'), ReadConfig().get_account('password'))
    # 进入电子班牌系统
    login.goto_electroniclassbrand()
    # 进入云控中心(县级管理)
    login.goto_cloudcontrolcenter()
    # 进入班级管理
    login.goto_classmanagemant()
    login.driver.refresh()

    # 课表设置
    login.goto_classschedule()
    login.import_classschedule("导入课表成功.xlsx")
    # login.import_classschedule("导入空课表成功.xlsx")

    # login.import_temporaryschedule("导入临时课表成功.xlsx")

    # #信息设置
    # login.goto_classinfo()
    # # #设置班级口号及班主任寄语
    # login.set_classmotto("好好学习")
    # sleep(1)
    # login.set_sendword("goodgoodstudy")
    # sleep(1)
    # # 设置,导入,智能分配值日生
    # login.set_dutystudent()
    # sleep(1)
    # login.import_dutystudent("导入值日生成功.xlsx")
    # sleep(1)
    # login.auto_distribution_dutystudent()
    # # 设置班级之星
    # login.set_classstar()
    # # 返回班级管理页面
    # login.backtrack()

    # login.quit()
